package cn.k23.bymain.controller.sys;

import cn.k23.bymain.bean.common.PageBean;
import cn.k23.bymain.bean.common.ResultBean;
import cn.k23.bymain.bean.sys.User;
import cn.k23.bymain.common.validation.group.Login;
import cn.k23.bymain.controller.common.BaseController;
import cn.k23.bymain.service.sys.UserService;
import cn.k23.interceptor.TokenInterceptor;
import cn.k23.utils.TokenUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: k23
 * @Date: 2022-05-06 15:01
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/user")

//Swagger注解
@Api(tags = "用户管理模块")

@Slf4j
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @Autowired(required = false)
    TokenUtils tokenUtils;

    //Swapper注解直接使用在类的上面
    @ApiOperation(value = "查询用户接口",notes = "查询用户列表，分页显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "当前第几页",required = false,dataType = "java.lang.Integer",paramType = "body"),
            @ApiImplicitParam(name="pageSize",value = "每页显示的记录数",required = false,dataType = "java.lang.Integer",paramType = "body"),
    })
    @ApiResponse(response = ResultBean.class,code = 200,message = "查询")
    @PostMapping ("/query")
    //@ApiIgnore 忽略表示这个方法或者类被忽略 在Swaager的请求参数不显示map 显示pageNum pageSize
    public ResultBean query(@RequestBody Map map){
        PageBean pageBean = userService.query(map);
        return success(pageBean);
    }


    //Swapper注解直接使用javabean上(User实体类上)
    @ApiOperation(value = "增加用户接口",notes = "添加用户")
    @PostMapping("/save")
    public ResultBean save(@Valid @RequestBody User user){ //@Vaild 校验参数注解
        int rs = userService.save(user);
        return rs>0?success("保存成功"):fail("保存失败");
    }

    //可生成token
    @ApiOperation(value = "/login生成token",notes = "生成token")
   @GetMapping("/login")
    public ResultBean login(){
        Map map = new HashMap();
        map.put("username","kk");
        map.put("password","987456");
        String token = tokenUtils.generateToken(map);
        return success("登录成功",token);
    }

    //参数验证测试
    @ApiOperation(value = "/lv参数验证",notes = "参数验证")
    @PostMapping("/lv")
    public ResultBean loginVerify(@RequestBody @Validated(Login.class) User user){
        return success("登录成功","sss");
    }

}
